<?php
	App::import("Helper","Html");
	class AppHtmlHelper  extends HtmlHelper
	{
        var $helpers = array("Html");
		function url($url = null, $full = false, $options = array())
		{
			$dynamicField = cr('DynamicPage');
			if(isset($options['dynamicPage']))
			{
				$dynamicField = $options['dynamicPage'];
				unset($options['dynamicPage']);
			}
			$RouterIns = Router::getInstance();
			$oldAdmin = $RouterIns->__admin;
			if($dynamicField)
			{
				$urlPrefix = cri("DynamicPage")->getPagePerfixUrl($dynamicField['field'],$dynamicField['value']);
				if(is_array($url))
				{
					
					$RouterIns->__admin = $urlPrefix;
					$url[$urlPrefix] = true;
				}else if(substr($url,0,1) == "/")
				{
					$url = $urlPrefix.$url;
				}
			}
			$newUrl = parent::url($url,$full,$options);
			if($dynamicField && is_array($url))
			{
				$RouterIns->__admin = $oldAdmin;
			}
			return $newUrl;
		}
        //Delegate to Html object so that compression can be handled.
		function css($path, $rel = null, $htmlAttributes = array(), $inline = true)
        {
          return $this->Html->css($path, $rel , $htmlAttributes , $inline);
        }
		function beforeRender()
		{
			parent::beforeRender();
			if(!isset($this->Javascript))
			{
				$this->initHelper("Javascript");
			}
			$this->css('/cms/css/jquery.autocomplete.css',null,array(),false);
			$this->Javascript->link('/cms/js/jquery.autocomplete.js',false);
			$View = $this->getView();
			if(is_object($View))
			{
				$ie6js = $this->Html->url('/js/ie6_warning.js');
				$View->addScript('<!--[if lte IE 7]><script src="'.$ie6js.'"></script><script>window.onload=function(){setTimeout(function(){e(URL+"/img/ieupgrade/")},1)}</script><![endif]--> <!--[if lte IE 6]><script src="'.$ie6js.'"></script><script>window.onload=function(){setTimeout(function(){e(URL+"/img/ieupgrade/")},1)}</script><![endif]-->');		
				if(cr('GeneralConfig.analytics_code'))
				{
					$View->addScript("analytics_code",cr('GeneralConfig.analytics_code'));
				}
			}
			$seeMore = $this->Javascript->object(__("See More"));
			$seeLess = $this->Javascript->object(__("See Less"));
			$js =<<<JS
			if(typeof ContentFlay == "object")
			{	ContentFlay.moreText = $seeMore;
				ContentFlay.lessText = $seeLess;
			}
JS;
			$this->Javascript->startup($js);
		}
		
		function afterRender()
		{
            //Migration Url exception.
            $isMigrationUrl = $this->params['controller'] == "migration";
			if(((isset($this->params['admin']) && $this->params['admin'] == 1) || (isset($this->params['requested']) && $this->params['requested'] == 1)) || !cr("cmsInstalled") || $isMigrationUrl)
			{
				return;
			}
			$View = $this->getView();
			$metaInformation = $View->getVar("metaInformation");
			if(!$metaInformation)
			{
				$metaInformation = array('pageTitle'=>$View->getVar("title_for_layout"),'metaDescription'=>$View->getVar("metaDescription"),'metaTags'=>$View->getVar("metaTags"));
				array_filter($metaInformation);
				foreach($metaInformation as $metaKey => $metaInfo)
				{
					if(strlen($metaInfo) == 0)
					{
						unset($metaInformation[$metaKey]);
					}
				}
			}
			if(!$metaInformation)
			{
				$ctrl = $View->params['controller'];
				$action = $View->params['action'];
				if($ctrl == "pages" && $action == "display")
				{
					$action = $View->getVar('page');
				}
				$metaInformation = cri("MetaInformation")->getUrlMetaInformation($ctrl,$action);
			}
			$metaKeys = array('pageTitle','metaDescription','metaTags');
			$defaultInformation = cri("MetaInformation")->getPageMetaInformation("default");
			foreach($metaKeys as $metaKey)
			{
				if((!isset($metaInformation[$metaKey]) || strlen($metaInformation[$metaKey]) == 0 ) && isset($defaultInformation[$metaKey]))
				{
					$metaInformation[$metaKey] = $defaultInformation[$metaKey];
				}
				if(isset($metaInformation[$metaKey]))
				{
					if($metaKey == "metaTags")
					{
						$View->set("metaKeywords",$metaInformation[$metaKey]);
					}else if($metaKey == "pageTitle" && strlen(trim($metaInformation[$metaKey])) > 0)
					{
						$View->set("pageTitleTag",$metaInformation[$metaKey]);
					}
					else
					{
						$View->set($metaKey,$metaInformation[$metaKey]);			
					}
				}	
			}
			parent::afterRender();
		}
		
		function flayText($content,$length = 100)
		{
            
			$id = "flay_text_content_id_".rand(1,400);
			$out = "<div id=\"$id\" class=\"flay_text_content\">$content</div>";
			if(!isset($this->Javascript))
			{
				$this->initHelper("Javascript");
			}
			$js= <<<END
			ContentFlay.implement(\$('#$id'),$length);
END;
			$this->Javascript->startup($js);
			return $out;
		}
		
		function shareLinks()
		{
			$this->getView()->addScript('sharelinks','<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>');
			return "<span class='st_sharethis_large' displayText='ShareThis'></span>
<span class='st_facebook_large' displayText='Facebook'></span>
<span class='st_twitter_large' displayText='Tweet'></span>
<span class='st_linkedin_large' displayText='LinkedIn'></span>
<span class='st_pinterest_large' displayText='Pinterest'></span>";
		}
	}
?>